CONTENTS | INDEX | PREV | NEXT
fopen
NAME
fopen - open a file and create a file pointer for it
SYNOPSIS
#include <stdio.h>
FILE *fp = fopen(filename, modes)
char *filename;
char *modes;
FUNCTION
fopen is the grand master of stdio; It opens and possibly creates a
file and returns a new file pointer for use by the program.
The first argument is the file to open, the second is a string
containing one or mode characters defined as follows:
r open for reading, the file must already exist
w open for writing, the file is created if it does not exist,
truncated if it does
a open for append, writes always append to the file. If 'a' is
ever specified, the file starts out positioned at the end
instead of at the beginning. This mode also creates the
file but only if it does not already exist.
+ along with 'r' this also allows writing to the file
along with 'w' this also allows reading from the file
b open for binary read/write, else the file is assumed to contain
text (this is ignored by DICE since there is no difference on
the Amiga).
All combinations except "rw" are allowed. One uses "r+" or "w+"
instead of "rw". By the above description "r+" is used to update
an existing file while "w+" is used to create a new file and then
allow reads as well as writes to it.
"wa" is equivalent to creating a new file and then appending to
it. "r+a" is equivalent to appending to an already existing
file.
Other examples of valid modes combinations: "r+b", "w+b", "rb", "wb",
"ab", "w", "r", "r+", "a", etc...
NOTE
refer to the file_pointer manual page for general information
EXAMPLE
/*
* create a new file and write some text to it
*/
#include <stdio.h>
#include <assert.h>
main()
{
FILE *fp = fopen("T:xx", "w");
char buf[256];
assert(fp);
puts("Enter a line");
if (fgets(buf, sizeof(buf), stdin)) {
fputs(buf, fp);
puts("The line has been written to T:xx");
}
fclose(fp);
return(0);
}
INPUTS
char *filename; file name to open
char *modes; open modes string
RESULTS
FILE *fp; new file pointer
SEE ALSO
fdopen, fopen, fclose, open, close